<?php

require_once 'classes/LightPressPlugin.php';

class CategoryStyle extends LightPressPlugin {
    
    var $default_context = LP_CONTEXT_ALL;
    var $active = true;
    var $hooks = array('parse_post');

    var $description = 'Changes stylesheet based on the first displayed post\'s category OR the short name of a static post';
    var $constructor_args = array('default_name' => 'Style to use if no posts found');
    var $hide_missing = 1;
    var $unique_key = 1;

    var $default_name = 'default';
    
    var $_frontend;
    
    var $_isdone = false;
	
    function CategoryStyle(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function hide() {
        $this->_frontend->tpl->setVar('PLUGIN_CATEGORYSTYLE', $this->default_name);
    }

    function run($hook, &$post) {
		if (!$this->active)
			return ($this->hide());
        elseif ($this->_isdone)
            return;
			
        $tpl =& $this->_frontend->tpl;
		$context =& $this->_frontend->context;
		$categories =& $this->_frontend->getCategories();
        
		switch ($context['type']) {
			case 'static':
				//use name of static page
				$pieces = explode('-', $context['filter']);
				$style = $pieces[0] . (isset($pieces[1]) ? '-' . $pieces[1] : '');
                break;
				
			case 'category':
				//use supplied category name
				if (isset($categories[$context['filter']]['category_nicename']))
                    $style = $categories[$context['filter']]['category_nicename'];
				break;
			
			default:
				//use first category of first post
				if (isset($post['categories']) && is_array($post['categories'])) {
					foreach ($post['categories'] as $post_category) {
						$style = $categories[$post_category]['category_nicename'];
						break;
					}
				}
				break;
		}
		if (empty($style))
			$style = $this->default_name;
		$tpl->setVar('PLUGIN_CATEGORYSTYLE', $style);
		$this->_isdone = true;
	}
	
}

?>